package com.woniuxy.service.impl;


import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.dao.GymInfoDao;
import com.woniuxy.entity.ChooseCourse;
import com.woniuxy.entity.CoachFreeday;
import com.woniuxy.entity.CoachInfo;
import com.woniuxy.entity.Course;
import com.woniuxy.entity.Focus;
import com.woniuxy.entity.GymCoach;
import com.woniuxy.entity.GymInfo;
import com.woniuxy.entity.Style;
import com.woniuxy.entity.User;


import com.woniuxy.service.CoachInfoService;

import com.woniuxy.service.GymInfoService;
import com.woniuxy.util.CommonUtil;

import javax.annotation.Resource;
/**
 * 场馆详细信息service实现
 * 
 * @author 陈一珩
 *
 */
@Service
public class GymInfoServiceImpl implements GymInfoService{
	
	private static final String User = null;
	@Autowired
	private GymInfoDao gymInfoMapper;

	@Override
	public GymInfo selectAllGymInfoByUserId(Integer userId) {

		return gymInfoMapper.selectAllGymInfoByUserId(userId);
	}
	@Autowired
	private CoachInfoService coachInfoService;
	@Transactional
	@Override
	public List<CoachInfo> selectAllGymCoachByUserId(Integer userId) {
		List<CoachInfo> coaches = new LinkedList<>();
		List<GymCoach> selectAllGymCoachByUserId = gymInfoMapper.selectAllGymCoachByUserId(userId);
		
//		while(iterator.hasNext()) {
//			System.out.println("进入循环");
//			System.out.println(iterator.next());
//			long userIdCoach = iterator.next().getUserIdCoach();
//			CoachInfo selectCoachInfoByUserId = coachInfoService.selectCoachInfoByUserId((int) userIdCoach);
//			coaches.add(selectCoachInfoByUserId);
//		}
		for(int i=0;i<selectAllGymCoachByUserId.size();i++) {
			long userIdCoach = selectAllGymCoachByUserId.get(i).getUserIdCoach();
			System.out.println(userIdCoach);
			CoachInfo selectCoachInfoByUserId = coachInfoService.selectCoachInfoByUserId((int) userIdCoach);
			coaches.add(selectCoachInfoByUserId);
		}
		System.out.println(coaches);
		return coaches;
	}
	@Override
	public Integer updateGymCoachNoSign(Integer gymId, Integer coachId) {
		
		return gymInfoMapper.updateGymCoachNoSignByUserId(gymId, coachId);
	}
	@Override
	public Integer updateGymCoachSign(Integer gymId, Integer coachId) {
		
		return gymInfoMapper.updateGymCoachSignByUserId(gymId, coachId);
	}
	@Transactional
	@Override
	public Integer updateGymInfo(GymInfo gymInfo,User user) {
		User currentUser = CommonUtil.getCurrentUser();
		long user_id = currentUser.getId();
		System.out.println("进入updateGymInfo Service"+user_id);
		HashMap<String, Object> hashMap = new HashMap<>();
		hashMap.put("user_id",(int) user_id);
		if(gymInfo.getMail().equals(null)) {
			hashMap.put("mail","default");
		}else {
			hashMap.put("mail",gymInfo.getMail());
		}
		
		hashMap.put("gymInfoStatus",gymInfo.getGymInfoStatus());
		
		
		return gymInfoMapper.updateGymInfo(hashMap);
	}
	@Transactional
	@Override
	public Integer addGymImg(String imgName) {
		User currentUser=(User) SecurityUtils.getSubject().getPrincipal();
		long id = currentUser.getId();
		String newImg="../../gymImg/"+imgName;
		
		return gymInfoMapper.updateGymImg(newImg,id);
	}
	@Transactional
	@Override
	public List<CoachInfo> selectMySginCoachesByLimit4(Integer id) {
		List<CoachInfo> coaches = new LinkedList<>();
		List<GymCoach> selectMySginCoachesByLimit4 = gymInfoMapper.selectMySginCoachesByLimit4(id);
		for(GymCoach ls:selectMySginCoachesByLimit4) {
			//System.out.println("进入循环");
			long userIdCoach = ls.getUserIdCoach();
			CoachInfo selectCoachInfoByUserId = coachInfoService.selectCoachInfoByUserId((int) userIdCoach);
			coaches.add(selectCoachInfoByUserId);
		}
		return coaches;
	}
	@Transactional
	@Override
	public List<CoachInfo> myPreEmp(Integer id) {
		List<CoachInfo> coaches = new LinkedList<>();
		List<GymCoach> myPreEmp = gymInfoMapper.myPreEmp(id);
		for(GymCoach ls:myPreEmp) {
			//System.out.println("进入循环");
			long userIdCoach = ls.getUserIdCoach();
			CoachInfo selectCoachInfoByUserId = coachInfoService.selectCoachInfoByUserId((int) userIdCoach);
			coaches.add(selectCoachInfoByUserId);
		}
		return coaches;
	}
	@Transactional
	@Override
	public Integer deleteSginCoach(Integer id, Integer coachId) {
		System.out.println("id:"+id+"coachId:"+coachId);
		Integer deleteGymCoach = gymInfoMapper.deleteGymCoach(id,coachId);
		return deleteGymCoach;
	}
	@Override
	public List<Focus> focusMe(Integer id) {
		List<Focus> focusMe = gymInfoMapper.focusMe(id);
		return focusMe;
	}
	@Transactional
	@Override
	public Integer arrangeCourse(ChooseCourse chooseCourse) {
		int freeTime = chooseCourse.getTime();
		int[] a=new int[1];
		a[0]=freeTime;
		int freeSun=CommonUtil.getFreeSun(a);
		chooseCourse.setTime(freeTime);
		Integer arrangeCourse = gymInfoMapper.arrangeCourse(chooseCourse);
		
		long coachId=chooseCourse.getUserIdCoach();
		
		CoachFreeday freeTime1=gymInfoMapper.getFreeTimeByid((int)coachId,chooseCourse.getDate());
		long free=freeTime1.getFreeSum();
		freeTime1.setFreeSum(free-freeSun);
		Integer updateFreeTimeByid = gymInfoMapper.updateFreeTimeByid(freeTime1);
		return updateFreeTimeByid;
	}
	@Override
	public List<Style> allStyles() {
		// TODO Auto-generated method stub
		return gymInfoMapper.allStyles();
	}
	
	@Override
	public List<Course> allCourses() {
		// TODO Auto-generated method stub
		return gymInfoMapper.allCourses();
	}
	@Override
	public List<CoachFreeday> freeDayByCoachId(Long id) {
		// TODO Auto-generated method stub
		return gymInfoMapper.freeDayByCoachId(id);
	}
	@Override
	public List<ChooseCourse> chooseCoursesById(Long id) {
		// TODO Auto-generated method stub
		return gymInfoMapper.allChooseCoursesByGid(id);
	}
	

}
